#include "FI_CNEXData.h"
#include <core/time/time.h>
#include <msgcenter/FIMsgProcess.h>
#include <qbprotocol/include/rdProto.h>
#include <qbprotocol/include/SSQBAModel.h>
#include <qbtools/message/MessagePoster.h>
#include <QDebug>

void FICNEXData::reqData(MessageReceiver* msgRecv) {
    qb::SSRequestMsg msg;
    msg.m_pRequest = nullptr;
    {
        msg.m_FuncID = E_FID_QBA_CNEX_INDEX_REQ;
        MessagePoster poster(&msg, qbmsg::fixedincome::EncodeFunc);
        poster.send(msgRecv);
    }

    {
        msg.m_FuncID = E_FID_QBA_CNEX_INDEX_PUSH;
        MessagePoster poster(&msg, qbmsg::fixedincome::EncodeFunc);
        poster.send(msgRecv);
    }
}

bool FICNEXData::onReceive(int32_t funcId, const qb::SSAckMsg& msg) {
    switch (funcId) {
    case E_FID_QBA_CNEX_INDEX_REQ:
    case E_FID_QBA_CNEX_INDEX_PUSH: {
        SCOPED_MUTEX(m_lock);
        m_datas.clear();
        xQBACNEXList_c* pdata = (xQBACNEXList_c*)msg.m_pRet;
        m_datas.reserve(pdata->m_CNEXList.size());
        tCNEXData data;
        for(const auto& unit : pdata->m_CNEXList) {
            data.time = unit.m_CNEXTimeNode;
            data.AllMarket = unit.m_CNEXAllMarket;
            data.BigBank = unit.m_CNEXAllBank;
            data.SmallBank = unit.m_CNEXSmallBank;
            data.NotBank = unit.m_CNEXNotBank;
            m_datas.push_back(data);
        }
        return true;
    } break;
    default:
        break;
    }
    return false;
}

QVector<FICNEXData::tCNEXData> FICNEXData::getData() const
{
    SCOPED_MUTEX(m_lock);
    return m_datas;
}
